package com.ocom.accesservice.controller;


import com.ocom.accesservice.service.MjQrListService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.master.EntityMjQrList;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.wx.QrPageOnLoadRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class QrCodeController {


    @Value("${mj_qr_key}")
    private String mj_qr_key;

    @Autowired
    MjQrListService mjQrListService;

    @PostMapping(AccessServiceUrl.URL_newQrCode)
    public Result newQrCode(@RequestBody  QrPageOnLoadRequest request){
        String fun="1";
        if(request.getFunction().equals("101")){
            fun="2";
        }

        String salt= RandomUtil.getRandomString(10);
        String timestamp= String.valueOf(new Date().getTime()).substring(0,10);
        String QrCode="OASC"+fun+salt+timestamp;

        String QrCodeMd5 = MD5Util.stringToMD5(QrCode+mj_qr_key);

        EntityMjQrList entityMjQrList=new EntityMjQrList();
        entityMjQrList.setComId(request.getCompanyId());
        entityMjQrList.setQrCode(QrCode+QrCodeMd5);
        entityMjQrList.setCreateTime(new Date());
        entityMjQrList.setDeviceNo(request.getMachNo());
        entityMjQrList.setPId(request.getPId());
        entityMjQrList.setResultCode(0);
        entityMjQrList.setQrFunction(request.getFunction());

        long curren = System.currentTimeMillis();
        curren += 30 * 60 * 1000;
        Date da = new Date(curren);

        entityMjQrList.setEndTime(da);

        mjQrListService.saveOne(entityMjQrList);

        return ResultUtil.success(entityMjQrList);
    }

    @PostMapping(AccessServiceUrl.URL_pollingQrCode)
    public Result pollingQrCode(String qrCode ,Integer pollingTimes){

        if(qrCode.length()!=57){
            return ResultUtil.error(ResultEnum.Err_202);
        }

        String orginCode=qrCode.substring(0,25);
        String md5Code=qrCode.substring(25,57);

        String SecCode=MD5Util.stringToMD5(orginCode+mj_qr_key);
        if(!md5Code.equals(SecCode)){
            return ResultUtil.error(ResultEnum.Err_202);
        }

        EntityMjQrList entityMjQrList=mjQrListService.searchQrCode(qrCode);
        Map<String,Object>  map=new HashMap<>();
        if(entityMjQrList==null){
            //未过期
            map.put("result",9);
            map.put("hint","过期");
        }else{
            if(entityMjQrList.getEndTime().compareTo(new Date())>=0){
                //未过期
                map.put("result",1);
                map.put("hint","成功");
            }else {
                map.put("result",9);
                map.put("hint","过期");
            }
        }
        return ResultUtil.success(map);
    }




}
